﻿var isCurrentPasswordValid;
var isPasswordDifferent;
var isPasswordValid;
var arePasswordsEqual;
var scriptCancel;
var authenticatedUrl;
var currentPasswordErrorMessage;
var passwordRepeatedErrorMessage;
var passwordConfirmationErrorMessage;

isCurrentPasswordValid = false;
isPasswordDifferent = false;
isPasswordValid = false;
arePasswordsEqual = false;
authenticatedUrl = "";
currentPasswordErrorMessage = "";
passwordRepeatedErrorMessage = "";
passwordConfirmationErrorMessage = "";

$(function () {




    $("#SendNewPassword").attr("disabled", true);

    $("#txtPasswordConfirmation").bind("blur", function () {
        ValidateConfirmation();
        SetSubmitButtonState();
    });

    $("#txtCurrentPassword").bind("blur", function () {
        $.ajax(
        {
            url: GetFullUrl("ValidatePassword"),
            data: { password: $("#txtCurrentPassword").val() },
            type: "POST",
            dataType: "json",
            cache: false,
            success: function (result) {
                if (result == true) {
                    ShowCustomValidationImage($("#txtCurrentPassword"), "");
                    isCurrentPasswordValid = true;
                }
                else {
                    ShowCustomValidationImage($("#txtCurrentPassword"), currentPasswordErrorMessage);
                    isCurrentPasswordValid = false;
                }
                SetSubmitButtonState();
            },
            error: function (xhr, text, err) { alert(text); }
        }
        );
    });


});

function GetFullUrl(url) {
    if (userIsLogged) {
        return authenticatedUrl + "Account/" + url;
    }
    else {
        return url;
    }
}

function SetSubmitButtonState() {
    $("#SendNewPassword").attr("disabled", !(isPasswordDifferent && isPasswordValid && arePasswordsEqual && isCurrentPasswordValid));
}

function OnPasswordChanged(resultStyle) {
    if (resultStyle == "strongPass" || resultStyle == "top_strongPass" || resultStyle == "goodPass" || resultStyle == "top_goodPass") {
        isPasswordValid = true;
    }
    else {
        isPasswordValid = false;
    }
    ValidateDifferent();
    ValidateConfirmation();
    SetSubmitButtonState();
}

function ValidateDifferent() {
    $.ajax(
        {
            url: GetFullUrl("ValidatePassword"),
            data: { password: $("#txtPassword").val() },
            type: "POST",
            dataType: "json",
            cache: false,
            success: function (result) {
                if (result == false) {
                    ShowCustomValidationImage($("#txtPassword"), "");
                    isPasswordDifferent = true;
                }
                else {
                    ShowCustomValidationImage($("#txtPassword"), passwordRepeatedErrorMessage);
                    isPasswordDifferent = false;
                }
            },
            error: function (xhr, text, err) { alert(text); }
        }
        );
}

function ValidateConfirmation() {
    if ($("#txtPassword").val() == $("#txtPasswordConfirmation").val()) {
        ShowCustomValidationImage($("#txtPasswordConfirmation"), "");
        arePasswordsEqual = true;
    }
    else {
        if ($("#txtPasswordConfirmation").val() != "") {
            ShowCustomValidationImage($("#txtPasswordConfirmation"), passwordConfirmationErrorMessage);
        }
        arePasswordsEqual = false;
    }
}


